<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Partner;
use App\Models\PartnerPrice;
use App\Models\PartnerType;

class PartnerController extends Controller
{
    public function index(Request $request)
    {
    	$where = $param = [];

        if($request->order=='asc') {
            $param['login_times'] = 'desc'; 
            $sort = 'asc';  
        } else {
            $param['login_times'] = 'asc';
            $sort = 'desc'; 
        }
        
    	$list = Partner::where($where)->orderBy('login_times',$sort)->paginate(20);
    	return view('admin.partner.index',compact('list','where','param'));
    }

    public function edit(Request $request)
    {
        $id = (int)$request->id;
        $data = Partner::find($id);
        return view('admin.partner.edit',compact('data'));
    }

    public function update(Request $request)
    {
        $id = (int)$request->id;
        $data = $request->except(['id','_token']);
        $flag = Partner::where(['id'=>$id])->update($data);
        
        return redirect('zadmin/partner/index');
    }

    public function destroy($id)
    {
        Partner::destroy($id);
        return redirect('zadmin/partner/index');
    }

    public function price(Request $request)
    {
    	$where = [];
    	$list = PartnerPrice::where($where)->paginate(20);
    	return view('admin.partner-price.index',compact('list'));
    }

    public function priceAdd(Request $request)
    {
    	$where = [];
    	$types = PartnerType::get();
    	return view('admin.partner-price.add',compact('types'));
    }

    public function priceSave(Request $request)
    {
        $data = $request->except(['_token']);
        if($request->pic) {
            $data['pic'] = '/'.$request->pic->store('uploads/'.date('Ymd'));    
        }
        //dd($data);    
        PartnerPrice::create($data);
        return redirect('zadmin/partner/price');
    }

    public function priceEdit(Request $request)
    {
        $where = [];
        $id    = (int)$request->id;
        $types = PartnerType::get();
        $data  = PartnerPrice::find($id);
        return view('admin.partner-price.edit',compact('types','data'));
    }

    public function priceUpdate(Request $request)
    {
        $data = $request->except(['_token']);
        $id   = (int)$request->id;
        if($request->pic) {
            $data['pic'] = '/'.$request->pic->store('uploads/'.date('Ymd'));    
        }
        PartnerPrice::where(['id'=>$id])->update($data);
        return redirect('zadmin/partner/price');
    }
}
